import os
import time
import win32gui
import win32api
import win32con

def click(hwd):
    win32gui.PostMessage(hwd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, 0)
    win32gui.PostMessage(hwd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, 0)

def SetText(hwd, text):
    win32api.SendMessage(hwd,win32con.WM_SETTEXT,None, text) 

def getText(hwd):
    buffer = '0' *50
    len = win32gui.SendMessage(hwd, win32con.WM_GETTEXTLENGTH)+1 #获取edit控件文本长度
    win32gui.SendMessage(hwd, win32con.WM_GETTEXT, len, buffer) #读取文本
    print("getText:",len,buffer[:len-1])

def proc(spwd):
    print('----proc start----')
    a = win32gui.FindWindow("#32770","设备登录")
    if a > 0:       
        pwd = win32gui.FindWindowEx(a, None, 'Edit', None)
        btn = win32gui.FindWindowEx(a, None, 'Button', None)
        bsetpwd = False        
        if pwd>0:
            #print("pwd:",pwd)
            pwd2 = win32gui.FindWindowEx(a, pwd, 'Edit', None)
            if pwd2>0:
                print("send password:",pwd2)
                SetText(pwd2,spwd)
                bsetpwd = True
        time.sleep(0.2)
        if bsetpwd and btn>0:
            #print("btn:",btn)
            text = win32gui.GetWindowText(btn)
            if text == "确定":
                print("send click:",btn)
                click(btn)
        #loginid = win32gui.GetWindowPlacement(a)
    else:
        print('Error:没有发现窗口')

if __name__ == "__main__":
    while True:
        proc("Sqt*7415369")
        time.sleep(1)
